#!/usr/bin/python3
# coding=utf-8
import json
import sys

###############################################################################
##  如前端入参是这个：
##  {
##    "实例IP":"192.168.1.101",
##    "时间阈值":"1",
##    "诊断时长":"10"
##    "目标磁盘":"vda"
##  }
##  解析参数方法：
##  sysak iosdiag latency -t 1 -T 10 vda
##  -t 1 时间阈值，表示要捕获IO耗时超过1ms的IO
##  -T 10 诊断时长，表示诊断10秒后自动退出
##  vda 目标磁盘，可以不填，不填默认会捕获系统所有磁盘的IO
##                如填写有效盘符则只捕获指定磁盘IO
##                如填写一个无效盘符，也会捕获系统所有磁盘的IO
##
###############################################################################
class Param(dict):
    def __missing__(self,key):
        sys.stderr.write("入参检查失败，没有检查到入参'%s'"%key)
        exit(1)

args = Param(json.loads(sys.argv[1]))

result = {}
result['commands'] = []

cmd0 = {}
cmd0['instance'] = args["实例IP"]
cmd0_arg_t = args.get("时间阈值","5000")
if int(cmd0_arg_t) < 0:
	cmd0_arg_t = 0
cmd0_arg_T = args.get("诊断时长","10")
if int(cmd0_arg_T) <= 0:
	cmd0_arg_T = 10
cmd0_arg_device = args.get("目标磁盘","")
dump_log_cmd = "cat /var/log/sysak/iosdiag/latency/result.log.stat 2>/dev/null;\
    echo \"\";cat /var/log/sysak/iosdiag/latency/result.log.seq 2>/dev/null;"
iosdiag_cmd = "sysak iosdiag latency -t "+str(cmd0_arg_t)+" -T "+str(cmd0_arg_T)+" "+cmd0_arg_device+" > /dev/null"
print_result_cmd = "if [ \"$?\" = \"0\" ]; then "+\
    dump_log_cmd+"else echo \"fail\"; fi"
cmd0['cmd'] = "rm /var/log/sysak/iosdiag/latency/* -f && "+iosdiag_cmd+" && "+print_result_cmd

result['commands'].append(cmd0)

data = json.dumps(result)
print(data)
